package com.dense.kuiniu.bookborrow.business.home.borrow;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.LanguageUtils;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.base.BaseFragment;
import com.dense.kuiniu.bookborrow.business.adapter.BookBorrowOrReturnResultAdapter;
import com.dense.kuiniu.bookborrow.business.entity.QueryBookInfo;
import com.dense.kuiniu.bookborrow.business.entity.ReaderInfo;
import com.dense.kuiniu.bookborrow.receiver.SystemTTS;
import com.dense.kuiniu.bookborrow.view.CountDownView;
import com.dense.kuiniu.bookborrow.view.CounterController;
import com.dense.kuiniu.bookborrow.view.SpaceItemDecoration;
import com.dense.kuiniu.bookborrow.xjSetting.config.ShareKeyConfig;
import com.dense.kuiniu.bookborrow.xjSetting.utils.SharePreferceUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rxjava.rxlife.RxLife;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.rxjava3.functions.Consumer;
import rxhttp.RxHttp;

/**
 * @author Administrator
 * @date 2020/8/25
 */
public class BorrowBookDelegate extends BaseFragment {
    private static final String BOOK_LIST = "list";
    private static final String CARD = "card";

    public static BorrowBookDelegate getInstance(String cardNo, ArrayList<QueryBookInfo> list) {
        Bundle bundle = new Bundle();
        bundle.putString(CARD, cardNo);
        bundle.putParcelableArrayList(BOOK_LIST, list);
        BorrowBookDelegate delegate = new BorrowBookDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    @BindView(R.id.count_down_view)
    CountDownView mCountDownView;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_card)
    TextView mTvCard;
    @BindView(R.id.tv_can_borrow_count)
    TextView mTvBorrowCount;
    @BindView(R.id.tv_state)
    TextView mTvState;
    private BookBorrowOrReturnResultAdapter mResultAdapter;
    private ArrayList<QueryBookInfo> mList;
    private String mCardNo;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mList = bundle.getParcelableArrayList(BOOK_LIST);
            mCardNo = bundle.getString(CARD);
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.delegate_book_borrow;
    }

    @Override
    protected void init() {
        queryReaderInfo();
        mTvTitle.setText(R.string.text_borrow_result);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(20));
        mRecyclerView.setAdapter(mResultAdapter = new BookBorrowOrReturnResultAdapter(R.layout.item_book_borrow_result, null, true));
        mResultAdapter.setEmptyView(R.layout.view_loading);
        int second = SharePreferceUtil.getInstance(getContext()).getInt(ShareKeyConfig.SAN_JI_CAIDAN,60);
        mCountDownView.setSeconds(second);
        mCountDownView.start(new CounterController.OnCompleteListener() {
            @Override
            public void onComplete() {
                exit();
            }
        });
    }

    @OnClick({R.id.tv_complete, R.id.tv_return})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        int id = view.getId();
        if (id == R.id.tv_complete) {
            exit();
        } else if (id == R.id.tv_return) {
            exit();
        }
    }


    private void exit() {
        pop();
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        mResultAdapter.setEmptyView(R.layout.view_empty);
        mResultAdapter.setNewInstance(mList);
        SystemTTS.getInstance().playText("借书完成，请拿走图书");
    }

    private void queryReaderInfo() {
        RxHttp.get("cslinks/rest/getReaderinforAll")
                .add("ReaderBarcode", mCardNo)
                .asString()
                .to(RxLife.toMain(this))
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Throwable {
                        Gson gson = new Gson();
                        ArrayList<ReaderInfo> list = gson.fromJson(s, new TypeToken<ArrayList<ReaderInfo>>() {
                        }.getType());
                        if (list != null && !list.isEmpty()) {
                            setReaderInfo(list.get(0));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {

                    }
                });
    }

    private void setReaderInfo(ReaderInfo info) {
        mTvName.setText(String.format("%s%s", getString(R.string.text_user_name), info.getName()));
        mTvCard.setText(String.format("%s%s", getString(R.string.text_card_no), info.getReaderCode()));
        mTvBorrowCount.setText(String.format(LanguageUtils.getCurrentLocale(),
                "%s%s", "已借数量：",
                info.getBorrowCount()));
        mTvState.setText(String.format("%s%s", getString(R.string.text_state), info.getState()));
    }
}